//
//  MyPageViewController.m
//  sellHouse
//
//  Created by 李 佳伟 on 2018/11/20.
//  Copyright © 2018年 李 佳伟. All rights reserved.
//

#import "MyPageViewController.h"
#import "JTCSM.h"
#import "MyPage_headerView.h"
#import "labelAndImageBtn.h"
#import "AlertShowView.h"
#import "AlertResultView.h"
#import "AlertSuccessView.h"
static CGFloat labelHeight = 50;
@interface MyPageViewController ()<MyPage_headerViewDelegate,AlertShowViewDelegate,UIAlertViewDelegate>
{
    MyPage_headerView *headerView;
     NSMutableArray *datasource;
    UILabel *_lgoinView;
    NSString *customPhone_number;
}
@end

@implementation MyPageViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [df isRemoveTarbar:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    customPhone_number = @"";
    [JTCSMSingle shareSingle].navigationC = self.navigationController;
    [JTCSMSingle shareSingle].thevc = self;
    // Do any additional setup after loading the view.
    self.view.backgroundColor = RGBACOLOR(241, 241, 241, 1);
    [self getKefuDianhuan];
    headerView = [[MyPage_headerView alloc]init];
    headerView.delegate = self;
    [self.view addSubview:headerView];
    [headerView setValueWithName:KHkNameString logo:@""];
    [df lineViewWithHeightofTop:KVIEW_Y(headerView)+10 superView:self.view lineHeight:10 space:0];
    datasource = @[
                @{@"image":@"people.png",@"title":@"手机号码",@"content":KHkTellString},
                @{@"image":@"phone.png",@"title":@"客服电话",@"content":@"13029243845"},
                @{@"image":@"building.png",@"title":@"关于我们",@"content":@""}].mutableCopy;
    for (int i = 0; i<datasource.count; i++) {
        NSDictionary *info =  datasource[i];
        labelAndImageBtn *view = [[labelAndImageBtn alloc]initWithFrame:CGRectMake(0, 10+KVIEW_Y(headerView)+labelHeight*i, KCONTROLLERWith, labelHeight) iamgeName:[info objectForKey:@"image"] title:[info objectForKey:@"title"] content:[info objectForKey:@"content"] Imagesize:20];
        view.tag = 1000+i;
        [self.view addSubview:view];
        if (i == 1) {
            [df addTapGestureRecognizerWithTarget:self action:@selector(callphone) andaddView:view];
        }
        if (i == 2) {
            [df addTapGestureRecognizerWithTarget:self action:@selector(gotoWebView) andaddView:view];
        }
    }
    
    
    labelAndImageBtn *view = [self.view viewWithTag:1002];
    _lgoinView = [UILabel initWithFrame:CGRectMake(Kspace, KVIEW_Y(view)+24, KSCREENWidth-2*Kspace, 44) text:@"退出登录" font:16 textAlignment:NSTextAlignmentCenter texcolor:[UIColor whiteColor] backgroundColor:RGBACOLOR(15,166,232,1)];
    [df layerWithView:_lgoinView radius:4 width:0 color:nil];
    [self.view addSubview:_lgoinView];
    _lgoinView.userInteractionEnabled = YES;
    self.view.userInteractionEnabled = YES;
    [df addTapGestureRecognizerWithTarget:self action:@selector(doExit) andaddView:_lgoinView];
     [self getKefuDianhuan];
    
    [AlertShowView shareSingle].delegate = self;
    [self.view addSubview:[AlertShowView shareSingle]];
    [self.view addSubview:[AlertResultView shareSingle]];
    [self.view addSubview:[AlertSuccessView shareSingle]];
    
}
//b编辑按钮代理事件
-(void)gotoEidt{
    [[AlertShowView shareSingle] show];
}
//提示框确定按钮代理事件
- (void)gotoTiJiaoWithChangeName:(NSString *)changename{
     [[AlertShowView shareSingle] hidden];
    if([changename isEqualToString:@""]){
        [[AlertResultView shareSingle] showWithsuccess:NO];
    }else{
        
        [self changeNameRequestWithName:changename];
        
    }
    
}
#pragma mark 修改姓名
- (void)changeNameRequestWithName:(NSString *)name{
//    hkapp/saleHouse/updateHkName
    
    [[LoadData object] loadDataPostAsynchronousWithURL:@"hkapp/saleHouse/updateHkName" withDict:@{@"phone":KHkTellString,@"token":KTokenString,@"hkName":name} superView:nil Block:^(id  _Nullable responseObject) {
        if (responseObject ==nil) {
//            [df showAlertWithTitle:@"提示" message:@"请求失败" btn1:@"知道了" btn2:nil btn3:nil delegate:nil tag:0];
            return ;
        }
        NSLog(@"resp:%@",responseObject);
        if ([[responseObject objectForKey:@"data"] integerValue]==1) {
            NSDictionary *dict = KUSERDEFAULTGET(KuserInfo);
            NSMutableDictionary *mudict = [NSMutableDictionary dictionaryWithDictionary:dict];
            [mudict setValue:name forKey:@"hkName"];
            KUSERDEFAULTSET(mudict, KuserInfo);
            [self->headerView setValueWithName:name logo:@""];
            [[AlertSuccessView shareSingle] show];
        }else{
           [[AlertResultView shareSingle] showWithsuccess:NO];
        }
        
    }];
}
#pragma mark 拨打电话
- (void)callphone{
    NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"tel:%@",customPhone_number];
    
    UIWebView * callWebview = [[UIWebView alloc] init];
    
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    
    [self.view addSubview:callWebview];
}

#pragma mark 关于我们
- (void)gotoWebView{
    WebViewController *vc = [[WebViewController alloc]init];
    vc.urlStr = KWEBURLHEADER(@"AboutUs.html");
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 退出t登录
- (void)doExit{

    [df showAlertWithTitle:@"确定退出登录?" message:@"" btn1:@"取消" btn2:@"确定" btn3:nil delegate:self tag:1];
}

#pragma mark 客服电话请求
- (void)getKefuDianhuan{
    [[LoadData object] loadDataPostAsynchronousWithURL:@"sysconf/getTell" withDict:@{} superView:nil Block:^(id  _Nullable responseObject) {
        labelAndImageBtn *view = [self.view viewWithTag:1001];
        view.contentLabel.text = KTurnToString(KResponseData);
        customPhone_number = KTurnToString(KResponseData);
    }];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag ==1+KAlertVTag) {
        if (buttonIndex==1) {
            [[LoadData object] loadDataPostAsynchronousWithURL:@"app/mobile/apploginout" withDict:@{@"phone":KHkTellString} superView:nil Block:^(id  _Nullable responseObject) {
                KUSERDEFAULTRemoveAll(KuserInfo);
                KUSERDEFAULTRemoveAll(Khtml_user);
                [df gotoLoginVC];
            }];
        }
    }
    if (alertView.tag ==10000+KAlertVTag) {
        if (buttonIndex==0) {
            [df gotoLoginVC];
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
